SPELL_TYPE_ICE = 0
SPELL_TYPE_FIRE = 1
SPELL_TYPE_EARTH = 2
SPELL_TYPE_ENERGY = 3
SPELL_TYPE_HOLY = 4
SPELL_TYPE_DEATH = 5
SPELL_TYPE_PHYSICAL = 6
SPELL_TYPE_HEALING = 7

elementalSpell = {}
elementalSpell.onSpellTargetCreature = {}
elementalSpell.onSpellTargetTile = {}
elementalSpell.onCastLevelMagicValue = {}
elementalSpell.onCastLevelMagicValue.Self = {}

---- new spell functions
new_spell_elemental_table = {
	[SPELL_TYPE_ICE] = {
		[1] = { -- TRANSFORM
			--- furnitures
			[1714] = 7162,
			[1715] = 7162,
			[1716] = 7162,
			[1717] = 7162,
			[1728] = 7167,
			[1729] = 7168,
			[1775] = 7170,
			[1646] = 7164,
			[1647] = 7163,
			[1648] = 7166,
			[1649] = 7165,
			--- trees
			[8313] = 8139, --- 8314 fire?
			[2710] = 2697,
			[2700] = 2698,
			[2718] = 7020,
			[2719] = 7021,
			[2720] = 7022,
			[7024] = 7023,
			--- stones
			[3607] = 6999,
			[3615] = 7000,
			[3608] = 7001,
			[3616] = 7002,
			[3609] = 7003,
			--- campfire
			[1423] = 1426,
			[1424] = 1426,
			[1425] = 1426,
			--- fire field
			[1492] = 1493,
			[1493] = 1494,
			--- other
			[3698] = 3697
			
		},
		[0] = { -- REMOVE
			1494
		},
		[2] = { -- SPECIAL SCRIPTS
		
		}
	},
	[SPELL_TYPE_FIRE] = {
		[1] = { -- TRANSFORM
			--- furnitures
			[7162] = 1714,
			[7167] = 1728,
			[7168] = 1729,
			[7170] = 1775,
			[7164] = 1646,
			[7163] = 1647,
			[7166] = 1648,
			[7165] = 1649,
			--- trees
			[8139] = 8313, --- 8314 fire?
			[8313] = 8314,
			[2697] = 2710,
			[2698] = 2700,
			[7020] = 2718,
			[7021] = 2719,
			[7022] = 2720,
			[7023] = 7024,
			--- stones
			[6999] = 3607,
			[7000] = 3615,
			[7001] = 3608,
			[7002] = 3616,
			[7003] = 3609,
			--- campfire
			[1426] = 1423,
			[1425] = 1423,
			[1424] = 1423,
			[1422] = 1423,
			--- fire field
			[1493] = 1492,
			[1494] = 1493,
			--- other
			[3697] = 3698,
			[2643] = 2238 -- burn boots
			
			--- skull candle
		},
		[0] = { -- REMOVE
			2246, 2374, 2070, 7729, 1947, 2597, 5878, 7426, 2694, 2245, 1989,
			--- wooden trash
			2250, 2251, 2252, 2253, 2254, 2255,
			--- resources
			5901, 2222, 5909, 5910, 5911, 5912, 5913, 5914, 11236,
			--- tools
			2549, 2556, 2324, 2551,
			--- eq
			2649, 2120, 2650, 2467, 2512, 2382, 2544
		},
		[2] = { -- SPECIAL SCRIPTS
			
			-- create fire bug
			[11396] = function (cid, pos, stackNumber)
				local topItem = getThingfromPos({x = pos.x, y = pos.y, z = pos.z, stackpos = stackNumber})
				if topItem.type == 0 then
					return doTransformItem(topItem.uid, 5468)
				end
				return false
			end,
			
			-- fire bug script
			[5466] = function (cid, pos, stackNumber)

				local POS
				local t
				for _, table_pos in pairs({{x = -1, y = -1}, {x = 0, y =  -1},{x = 1, y = -1}, {x = 1, y = 0},{x = 1, y = 1},{x = 0, y = 1},{x = -1, y = 1}, {x = -1, y = 0}}) do
					POS = {x = pos.x + table_pos.x, y = pos.y + table_pos.y, z = pos.z} 
					
					for a = 1, 10 do
						POS.stackpos = a
						t = getThingFromPos(POS)
						if t.itemid == 5466 then
							addEvent(new_spell_elemental_table[SPELL_TYPE_FIRE][2][5466], 2*1000, 0, {x = POS.x, y = POS.y, z = POS.z}, 1)
							doTransformItem(t.uid, t.itemid -1)
							doDecayItem(t.uid)
							break
						end
					end
				end
			end
		
		}
	},
	
	[SPELL_TYPE_EARTH] = {
		[1] = { -- TRANSFORM
			--- wheat
			[2737] = 2738,
			[2738] = 2739,
			--- jungle/swamp grass
			[2781] = 2782,
			[3984] = 3985
			
			--[5463] = 5471
		}
	},
	
	[SPELL_TYPE_PHYSICAL] = {
		[1] = { -- TRANSFORM
			
		},
		[0] = { -- REMOVE
		
		}
	}
}

--new_spell_elemental_table['ice'] = new_spell_elemental_table[SPELL_TYPE_ICE]
--new_spell_elemental_table['fire'] = new_spell_elemental_table[SPELL_TYPE_FIRE]

elemental_items_table = {
	[SPELL_TYPE_ICE] = {
		[0] = { }, --- negative
		[1] = {} --- positive, id
	},
	[SPELL_TYPE_ENERGY] = {
	--- negative = bigger dmg
		--- helmet
		[2461] = 1, --- leather helmet
		[7901] = 5, --- lightning headband
		
		[2458] = -2, --- chain helmet
		[2460] = -3, --- brass helmet
		[2480] = -4, --- legion helmet
		[2473] = -4, --- viking helmet
		[2459] = -5, --- iron helmet
		[2481] = -5, --- soldier helmet
		[2457] = -6, --- steel helmet
		
		--- armor
		[2467] = 4, --- leather armor
		[7898] = 11, --- lightning robe
		
		[2464] = -6, --- chain armor
		[2465] = -8, --- brass armor
		[2483] = -9, --- scale armor
		[2463] = -10, --- plate armor
		
		--- legs
		[2649] = 1, --- leather legs
		[7895] = 8, --- lightning legs
		
		[2648] = -3, --- chain legs
		[2478] = -5, --- brass legs
		[2647] = -7, --- plate legs
		
		--- boots
		[2643] = 1, --- leather boots
		[7893] = 2, --- lightning boots
		
		[2645] = -3, --- steel boots
		
		--- neakles
		[2161] = 1, --- strange talisman
		[7889] = 2, --- lightning pendant
		
		[2171] = -2, --- platinum amulet
		
		--- shield
		[2512] = 7, --- wooden shield [14]
		[2525] = 13, --- dwarven shield [26]
		
		[2511] = -8, --- brass shield [-16]
		[2510] = -8, --- plate shield [-17]
		[2530] = -9, --- copper shield [-19]
		[2509] = -10, --- steel shield [-21]
		[2513] = -11, --- battle shield [-23]
		
		--- waepons
			--- wands
			[2189] = 1, --- wand of cosmic energy
			[2190] = 1 --- wand of vortex
		
		--- others
	},
	[SPELL_TYPE_DEATH] = {
		[0] = { }, --- negative
		[1] = {} --- positive, id
	}
}

-----------
function onCastElementalSpellTargetTile(cid, pos, spellType)
	--- CALLBACK_PARAM_TARGETTILE
	local table_ = new_spell_elemental_table[spellType]
	if table_ == nil then
		return false
	end
	for i = 1, 5 do
		local topItem = getThingfromPos({x = pos.x, y = pos.y, z = pos.z, stackpos = i})
		if topItem.uid == 0 then
			break
		end
		
		if isCreature(topItem.uid) then
			--- next
			 
		elseif table_[1][topItem.itemid] ~= nil then
			doTransformItem(topItem.uid, table_[1][topItem.itemid])
		elseif isInArray(table_[0], topItem.itemid) then
			doRemoveItem(topItem.uid)
		elseif table_[2][topItem.itemid] ~= nil then
			table_[2][topItem.itemid](cid, pos, i)
		end
	end
	return true
end

function doDefaultSpellFormula(cid, level, maglevel, mina, minb, maxa, maxb)
	--[[
		setCombatFormula(combat, type, mina, minb, maxa, maxb)
		max = (int32_t)((player->getLevel() + player->getMagicLevel() * 4) * 1. * mina + minb);
		min = (int32_t)((player->getLevel() + player->getMagicLevel() * 4) * 1. * maxa + maxb);
	]]
	max = (getPlayerLevel(cid) +getPlayerMagLevel(cid)*4) * mina +minb
	min = (getPlayerLevel(cid) +getPlayerMagLevel(cid)*4) * maxa + maxb
	return min, max
end

function doDestroyContainer(item)
	if not isItemContainer(item.itemid) then
		return false
	end
	
	for slot = 0, getContainerSize(item.uid) -1 do
		itemX = getContainerItem(item.uid, slot)
		if itemX == nil then -- itemX.uid ?
			break
		end
		--- move item
	end
	doRemoveItem(item.uid)
	return true
end

--------------------------

--- SPELL_TYPE_ICE
function elementalSpell.onSpellTargetCreature.SPELL_TYPE_ICE(cid, target)
	if not doRemoveCondition(target, CONDITION_FIRE) then
		--- CONDITION_FREEZING
	end
	return true
end

function elementalSpell.onSpellTargetTile.SPELL_TYPE_ICE(cid, pos)
	onCastElementalSpellTargetTile(cid, pos, SPELL_TYPE_ICE)
	return true
end

function elementalSpell.onCastLevelMagicValue.SPELL_TYPE_ICE(cid, level, maglevel, min, max)
	return -min, -max
end


--- SPELL_TYPE_FIRE
function elementalSpell.onSpellTargetCreature.SPELL_TYPE_FIRE(cid, target)
	return true
end

function elementalSpell.onSpellTargetTile.SPELL_TYPE_FIRE(cid, pos)
	onCastElementalSpellTargetTile(cid, pos, SPELL_TYPE_FIRE)
	return true
end

function elementalSpell.onCastLevelMagicValue.SPELL_TYPE_FIRE(cid, level, maglevel, min, max)
	return -min, -max
end


--- SPELL_TYPE_EARTH
function elementalSpell.onSpellTargetCreature.SPELL_TYPE_EARTH(cid, target)
	--- poison
	return true
end

function elementalSpell.onSpellTargetTile.SPELL_TYPE_EARTH(cid, pos)
	--- plants++
	onCastElementalSpellTargetTile(cid, pos, SPELL_TYPE_EARTH)
	return true
end

function elementalSpell.onCastLevelMagicValue.SPELL_TYPE_EARTH(cid, level, maglevel, min, max)
	return -min, -max
end


--- SPELL_TYPE_ENERGY
function elementalSpell.onSpellTargetCreature.SPELL_TYPE_ENERGY(cid, target)
	if isPlayer(target) then
		local sum = 0
	
		local neg = 0
		local negCount = 0
		
		local poz = 0
		local pozCount = 0
		
		for i = CONST_SLOT_HEAD, CONST_SLOT_AMMO do
			local tmp = getPlayerSlotItem(cid, i)
			if tmp.itemid ~= nil and elemental_items_table[SPELL_TYPE_ENERGY][tmp.itemid] ~= nil then
				if elemental_items_table[SPELL_TYPE_ENERGY][tmp.itemid] < 0 then
					neg = neg + elemental_items_table[SPELL_TYPE_ENERGY][tmp.itemid]
					negCount = negCount +1
				else
					poz = poz + elemental_items_table[SPELL_TYPE_ENERGY][tmp.itemid]
					pozCount = pozCount +1
				end
			end
		end
		sum = neg * negCount + poz * pozCount
		
		if sum < 0 then
			doTargetCombatHealth(cid, target, COMBAT_ENERGYDAMAGE, sum, sum, CONST_ME_ENERGYAREA)
		end
	else
		--- discharge
	end
	return true
end

function elementalSpell.onSpellTargetTile.SPELL_TYPE_ENERGY(cid, pos)
	return true
end

function elementalSpell.onCastLevelMagicValue.SPELL_TYPE_ENERGY(cid, level, maglevel, min, max)
	return -min, -max
end


--- SPELL_TYPE_HOLY
function elementalSpell.onSpellTargetCreature.SPELL_TYPE_HOLY(cid, target)
	--- light
	return true
end

function elementalSpell.onSpellTargetTile.SPELL_TYPE_HOLY(cid, pos)
	return true
end

function elementalSpell.onCastLevelMagicValue.SPELL_TYPE_HOLY(cid, level, maglevel, min, max)
	--- light + haste
	return -min, -max
end


--- SPELL_TYPE_DEATH
function elementalSpell.onSpellTargetCreature.SPELL_TYPE_DEATH(cid, target)
	if isCreature(target) then
		-- healing undead
	end
	return true
end

function elementalSpell.onSpellTargetTile.SPELL_TYPE_DEATH(cid, pos)
	return true
end

function elementalSpell.onCastLevelMagicValue.SPELL_TYPE_DEATH(cid, level, maglevel, min, max)
	--- light off :)
	doRemoveCondition(cid, CONDITION_LIGHT)

	local mult = 1 -(getCreatureHealth(cid) / getCreatureMaxHealth(cid))
	min = min *(1 + mult)
	max = max *(1 + mult)
		
		--- 3*level - maglevel hp
		--- -2 hp
		
	doTargetCombatHealth(cid, cid, COMBAT_DEATHDAMAGE, -10, -10, CONST_ME_DRAWBLOOD)
	return -min, -max
end


--- SPELL_TYPE_PHYSICAL
function elementalSpell.onSpellTargetCreature.SPELL_TYPE_PHYSICAL(cid, target)
	--- bleeding
	return true
end

function elementalSpell.onSpellTargetTile.SPELL_TYPE_PHYSICAL(cid, pos)
	onCastElementalSpellTargetTile(cid, pos, SPELL_TYPE_PHYSICAL)
	return true
end

function elementalSpell.onCastLevelMagicValue.SPELL_TYPE_PHYSICAL(cid, level, maglevel, min, max)
	return -min, -max
end


--- SPELL_TYPE_HEALING
function elementalSpell.onSpellTargetCreature.SPELL_TYPE_HEALING(cid, target)
	return true
end

function elementalSpell.onSpellTargetTile.SPELL_TYPE_HEALING(cid, pos)
	return true
end

function elementalSpell.onCastLevelMagicValue.Self.SPELL_TYPE_HEALING(cid, level, maglevel, min, max)
	local summons = getCreatureSummons(cid)
	if #summons < 1 then
		return true
	end
	
	--- healing summons
	for _, summon in pairs(summons) do
		doTargetCombatHealth(cid, summon, COMBAT_HEALING, min, max, CONST_ME_MAGIC_GREEN)
	end
	return min, max
end

function elementalSpell.onCastLevelMagicValue.SPELL_TYPE_HEALING(cid, level, maglevel, min, max)
	--- add mana
	return min, max
end

--- CONDITION_PACIFIED



--[[
function onCastElementalSpellTargetCreature(cid, target, spellType)
	--- CALLBACK_PARAM_TARGETCREATURE
	if spellType == SPELL_TYPE_ICE then
		if not doRemoveCondition(target, CONDITION_FIRE) then
		--- CONDITION_FREEZING
		end
	end
	
	if isPlayer(target) then
		---- energy
	end
	return true
end
]]

function checkSpell(cid, spellType)
	return true
end
